package com.icss.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.icss.beans.TeacherTeamVO;
import com.icss.daos.TeacherTeamDao;

/**
 * @author 廖鹏
 * @version 创建时间：2010-3-16 下午03:32:59
 * 类说明 师资团队
 */
@SuppressWarnings("serial")
public class TeacherTeamServlet extends HttpServlet {
	
	int totalRecord=0;

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("jin lai le");
		String action=request.getParameter("action");
		if("init".equals(action)){
			initList(request,response);
		}
		if("totalRecord".equals(action)){
			totalRecordTeacher(request,response);
		}
		if("page".equals(action)){
			pageListAction(request,response);
		}
		if("list".equals(action)){
			TeacherListAction(request,response);
		}
		if("get".equals(action)){
			GetTeacherAction(request,response);
		}
		if("update".equals(action)){
			updateTeacherAction(request,response);
		}
		if("add".equals(action)){
			addTeacherAction(request,response);
		}
		if("delete".equals(action)){
			deleteTeacherAction(request,response);
		}
		if("set".equals(action)){
			setPublicAction(request,response);
		}
		
	}
	
	@SuppressWarnings("unchecked")
	private void TeacherListAction(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		PrintWriter out = response.getWriter();		
		
		int currentPage=1,pageSize=5;
		String currentPageS=request.getParameter("Page");
		String pageSizeS=request.getParameter("pageSize");
		if(null!=currentPageS){
			currentPage=Integer.parseInt(currentPageS);
		}
		if(null!=pageSizeS){
			pageSize=Integer.parseInt(currentPageS);
		}
		TeacherTeamDao teacherTeamDao=new TeacherTeamDao();
		List list= teacherTeamDao.getPageTeacherTeams(currentPage,pageSize);
		List<TeacherTeamVO> teacherList=(List<TeacherTeamVO>)list.get(0);
		int totaPage = (Integer)list.get(1);
		int totalRecord = (Integer)list.get(2);
		request.setAttribute("teacherList", teacherList);
		request.setAttribute("totalRecord", totalRecord);
		request.setAttribute("totalPage", totaPage);
		request.getRequestDispatcher("/page/teacher/teacher_list.jsp").forward(request, response);
		out.flush();
		out.close();
		
	}
	
	
	private void totalRecordTeacher(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		PrintWriter out=response.getWriter();
		TeacherTeamDao teacherTeamDao = new TeacherTeamDao();
		int i=teacherTeamDao.getTotalRecord();
		out.println(i);
		out.flush();
		out.close();
		
	}
	
	
	
	private void initList(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			IOException {

		PrintWriter out = response.getWriter();
		
		TeacherTeamDao teacherTeamDao = new TeacherTeamDao();
		List<TeacherTeamVO> teacherList = teacherTeamDao.getPageTeacherTeams();
		request.setAttribute("teacherList", teacherList);
		totalRecord=teacherTeamDao.getTotalRecord();
		request.setAttribute("totalRecord", totalRecord);
		request.getRequestDispatcher("/page/teacher/teacherList.jsp").forward(request, response);
		out.flush();
		out.close();
	}


	@SuppressWarnings("unchecked")
	private void pageListAction(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException{
		
		PrintWriter out = response.getWriter();		
		
		int currentPage=1,pageSize=5;
		String currentPageS=request.getParameter("Page");
		String pageSizeS=request.getParameter("pageSize");
		if(null!=currentPageS){
			currentPage=Integer.parseInt(currentPageS);
		}
		if(null!=pageSizeS){
			pageSize=Integer.parseInt(currentPageS);
		}
		TeacherTeamDao teacherTeamDao=new TeacherTeamDao();
		List list= teacherTeamDao.getPageTeacherTeams(currentPage,pageSize);
		List<TeacherTeamVO> teacherList=(List<TeacherTeamVO>)list.get(0);
		int totaPage = (Integer)list.get(1);
		int totalRecord = (Integer)list.get(2);
		request.setAttribute("page_teacherList", teacherList);
		request.setAttribute("totalRecord", totalRecord);
		request.setAttribute("totalPage", totaPage);
		System.out.println(teacherList.get(0).getTeacherName());
		request.getRequestDispatcher("/page/teacher/forward_kecheng.jsp").forward(request, response);
		out.flush();
		out.close();
	}
	
	private void GetTeacherAction(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		PrintWriter out=response.getWriter();
		
		int teacherID=Integer.parseInt(request.getParameter("teacherID"));
		TeacherTeamDao teacherTeamDao = new TeacherTeamDao();
		TeacherTeamVO teacherTeamVO = teacherTeamDao.getTeacherTeamVO(teacherID);
		
		request.setAttribute("teacherTeamVO", teacherTeamVO);
		request.getRequestDispatcher("/page/teacher/updateTeacher.jsp").forward(request, response);
		
		out.flush();
		out.close();
		
	}
	
	private void updateTeacherAction(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		PrintWriter out=response.getWriter();
		
		int teacherID=Integer.parseInt(request.getParameter("teacherID"));
		String teacherName = request.getParameter("teacherName");
		String position = request.getParameter("position");
		String introduction = request.getParameter("introduction");
		int visiable=Integer.parseInt(request.getParameter("visiable"));
		
		TeacherTeamVO teacherTeamVO = new TeacherTeamVO();
		
		teacherTeamVO.setTeacherID(teacherID);
		teacherTeamVO.setTeacherName(teacherName);
		teacherTeamVO.setPosition(position);
		teacherTeamVO.setIntroduction(introduction);
		teacherTeamVO.setVisiable(visiable);
		TeacherTeamDao teacherTeamDao = new TeacherTeamDao();
		boolean b= teacherTeamDao.updateTeacherTeam(teacherTeamVO);
		if(b){
			out.write("<script>alert('成功了')</script>");
			response.sendRedirect("TeacherTeamServlet?action=list");
		}
		out.flush();
		out.close();
		
	}
	
	
	
	private void addTeacherAction(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		PrintWriter out=response.getWriter();
		
		String teacherName = request.getParameter("teacherName");
		String position = request.getParameter("position");
		String introduction = request.getParameter("introduction");
		int visiable=Integer.parseInt(request.getParameter("visiable"));
		TeacherTeamVO teacherTeamVO = new TeacherTeamVO();
		
		teacherTeamVO.setTeacherName(teacherName);
		teacherTeamVO.setPosition(position);
		teacherTeamVO.setIntroduction(introduction);
		teacherTeamVO.setVisiable(visiable);
		TeacherTeamDao teacherTeamDao = new TeacherTeamDao();
		boolean b= teacherTeamDao.addTeacher(teacherTeamVO);
		if(b){
			out.write("<script>alert('成功了')</script>");
			response.sendRedirect("TeacherTeamServlet?action=list");
		}
		out.flush();
		out.close();
		
	}
	
	private void deleteTeacherAction(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out=response.getWriter();
		
		int teacherID=Integer.parseInt(request.getParameter("teacherID"));
				
		TeacherTeamDao teacherTeamDao = new TeacherTeamDao();
		boolean b= teacherTeamDao.delTeacherTeam(teacherID);
		if(b){
			out.write("<script>alert('成功了')</script>");
			response.sendRedirect("TeacherTeamServlet?action=list");
		}
		out.flush();
		out.close();		
		
	}
	
	
	private void setPublicAction(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		PrintWriter out=response.getWriter();
		
		int teacherID=Integer.parseInt(request.getParameter("teacherID"));
		int visiable = Integer.parseInt(request.getParameter("visiable"));
		
		TeacherTeamDao teacherTeamDao = new TeacherTeamDao();
		boolean b= teacherTeamDao.updateTeacherTeam(teacherID,visiable);
		if(b){
			out.write("<script>alert('成功了')</script>");
			response.sendRedirect("TeacherTeamServlet?action=list");
		}
		out.flush();
		out.close();
		
	}

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}
	
	

}
